//
//  FTQLoginController.m
//  xiangmu
//
//  Created by apple on 2019/8/14.
//  Copyright © 2019 rzt. All rights reserved.
//

#import "FTQLoginController.h"
#import "FTQRegController.h"
#import "DGPUtil.h"



@interface FTQLoginController ()
@property(strong,nonatomic)NSMutableArray* dataArr;
@end

@implementation FTQLoginController

-(void)viewWillAppear:(BOOL)animated{
    [self.dataArr removeAllObjects];
    [self.dataArr addObjectsFromArray:kUserDefaultObjectForKey(@"user")];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setHidden:YES];
    self.dataArr=[[NSMutableArray alloc] init];
    [self.dataArr addObjectsFromArray:kUserDefaultObjectForKey(@"user")];
    if (kUserDefaultObjectForKey(@"only")==nil&&![kUserDefaultObjectForKey(@"only") isEqualToString:@"2"]){
        NSDictionary* dic=[NSDictionary dictionaryWithObjectsAndKeys:@"Jackson", @"username", @"qqq123456", @"password",[self nowTimeSwitchTimestamp],@"date",@"暂无昵称",@"nickname",@"10000",@"money",@"无",@"zhifupwd",nil];
        [self.dataArr addObject:dic];
        kUserDefaultSetObjectForKey(self.dataArr,@"user");
        kUserDefaultSetObjectForKey(@"2",@"only");
    }
}

- (IBAction)regBtnTap:(id)sender {
    FTQRegController* reg=[[FTQRegController alloc] init];
    [self.navigationController pushViewController:reg animated:YES];
}

- (IBAction)loginBtnTap:(id)sender {
    if (self.usernameTextfield.text.length==0) {
        [self.view makeToast:@"请输入账号"
                    duration:2.0
                    position:CSToastPositionCenter];
        return;
    }
    if (self.passwordTextfield.text.length==0){
        [self.view makeToast:@"请输入密码"
                    duration:2.0
                    position:CSToastPositionCenter];
        return;
    }
    else if ([self checkUserName:self.usernameTextfield.text]) {
        MBProgressHUD* HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                        HUD.completionBlock = ^(){
                            
                            [self dismissViewControllerAnimated:YES completion:nil];
                        };
                        [HUD hideAnimated:YES afterDelay:1.0];
        return;
    }
    
    [self.view makeToast:@"账号或密码错误~"
                duration:2.0
                position:CSToastPositionCenter];
    
}

- (BOOL)checkUserName:(NSString *)userName {
    BOOL res = NO;
    for (NSDictionary *userInfo in self.dataArr) {
        if ([userInfo[@"username"] isEqualToString:userName]) {
            if ([self.passwordTextfield.text isEqualToString:userInfo[@"password"]]) {
                kUserDefaultSetObjectForKey(userInfo[@"date"],@"userid");
                NSInteger index = [self.dataArr indexOfObject:userInfo];
                kUserDefaultSetObjectForKey(@(index), @"number");
                res = YES;
                break;
            }
        }
    }
    
    return res;
    
}

- (IBAction)youkeBtnTap:(id)sender {
    NSDictionary* dic=[NSDictionary dictionaryWithObjectsAndKeys:@"Tourist", @"username", @"我是游客", @"password",[self nowTimeSwitchTimestamp],@"date",@"Tourist",@"nickname",@"0.00",@"money",@"无",@"zhifupwd",nil];
    [self.dataArr insertObject:dic atIndex:0];
    kUserDefaultSetObjectForKey(self.dataArr,@"user");
    kUserDefaultSetObjectForKey(dic[@"date"],@"userid");
    
    MBProgressHUD* HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.completionBlock = ^(){
        [self dismissViewControllerAnimated:YES completion:nil];
    };
    [HUD hideAnimated:YES afterDelay:1.0];
}

@end
